package com.shujia.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;

import java.io.IOException;

public class Demo7HbaseToHbase {


    public static class ReadhbaseMapper extends TableMapper<Text, IntWritable> {
        /**
         * @param key     hbase中的rowkey
         * @param value   : hbase中的一个rowkey对应的数据
         * @param context ： 上下文对象
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
            String id = Bytes.toString(key.get());


            //取出班级
            String clazz = Bytes.toString(value.getValue("info".getBytes(), "clazz".getBytes()));


            //输出到reduce端
            context.write(new Text(clazz), new IntWritable(1));

        }
    }

    public static class WriteHbaseReduce extends TableReducer<Text, IntWritable, NullWritable> {

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

            //统计班级人数
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }

            String clazz = key.toString();


            //构建put
            Put put = new Put(clazz.getBytes());


            //增加列
            put.addColumn("info".getBytes(), "num".getBytes(), (sum + "").getBytes());


            //写入hbase
            context.write(NullWritable.get(), put);

        }
    }


    public static void main(String[] args) throws Exception {

        Configuration configuration = new Configuration();

        //指定zk的连接地址
        configuration.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

        Job job = Job.getInstance(configuration);

        //指定map端
        TableMapReduceUtil.initTableMapperJob(
                "student",
                new Scan(),
                ReadhbaseMapper.class,
                Text.class,
                IntWritable.class,
                job
        );


        //指定reduce端
        //指定表名，需要先创建表  ， create 'clazz_num','info'
        TableMapReduceUtil.initTableReducerJob(
                "clazz_num",
                WriteHbaseReduce.class,
                job);


        job.waitForCompletion(true);

    }
}
